﻿/** \file
* \par Subversion URL:
* \$URL: http://planar-rendering.googlecode.com/svn/trunk/frontend/src/FunctionFactory.cs $
* \version \$Rev: 74 $
* \date    \$Date: 2009-04-20 01:12:01 +0000 (Mon, 20 Apr 2009) $
*/

using System;

namespace Frontend
{
	/// <summary>
	/// An abstract base for a factory class that produces functions.
	/// </summary>
	abstract class AbstractFunctionFactory
	{
		/// <summary>The integer index of this function type.</summary>
		public int enumIndex;

		/// <summary>
		/// Get a description of the function type associated with this factory.
		/// </summary>
		/// <returns>The description string.</returns>
		public abstract string GetDescription();

		/// <summary>
		/// Get a new instance of the function type associated with this factory.
		/// </summary>
		/// <param name="gen">The associated generator.</param>
		/// <returns>The new function instance.</returns>
		public abstract Function Instantiate(Generator gen);
	}

	/// <summary>
	/// A factory class that produces functions.
	/// </summary>
	/// <typeparam name="FuncType">The function type to produce.</typeparam>
	class FunctionFactory<FuncType>
		: AbstractFunctionFactory
		where FuncType : Function, new()
	{
		/// <summary>
		/// Get a description of the function type associated with this factory.
		/// </summary>
		/// <returns>The description string.</returns>
		public override string GetDescription()
		{
			// This is a horrible hack.
			return (new FuncType()).GetDescription();
		}

		/// <summary>
		/// Get a new instance of the function type associated with this factory.
		/// </summary>
		/// <param name="gen">The associated generator.</param>
		/// <returns>The new function instance.</returns>
		public override Function Instantiate(Generator gen)
		{
			Function newFunc = new FuncType();
			newFunc.SetRanges(gen);
			return newFunc;
		}
	}
}
